package yagwl.service.util.web;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;
import java.util.logging.Logger;

public class UrlUtil {
  private static final Logger logger = Logger
      .getLogger(UrlUtil.class.getName());

  public static String getUrl(String scheme, String server, int port, String uri) {
    StringBuffer buf = new StringBuffer();
    buf.append(scheme + "://");
    buf.append(server);
    if (scheme.equalsIgnoreCase("http") && port == 80
        || scheme.equalsIgnoreCase("https") && port == 443) {
    } else {
      buf.append(":" + port);
    }
    buf.append(uri);
    return buf.toString();
  }

  public static String getContent(String location) throws IOException {
    logger.info(location);
    URL url = new URL(location);
    InputStream stream = url.openStream();
    Scanner scanner = new Scanner(stream);
    scanner.useDelimiter("\\A"); // http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html
    String content = scanner.next();
    scanner.close();
    return content;
  }

}
